package edu.hawaii.ics.pjdb.result_transport;

import java.util.List;

import edu.hawaii.ics.pjdb.types.Tuple;

/**
 * Buffers all tuples it receives in a list. This class is thread safe.
 * 
 * @author chris
 * 
 */
public class BufferingTupleListener extends TupleListener {

    public static class Builder extends TupleListener.Builder {
        private final List<Tuple> buffer;

        public Builder(List<Tuple> buffer) {
            this.buffer = buffer;
        }

        @Override
        public TupleListener create() {
            return new BufferingTupleListener(buffer);
        }
    }

    private final List<Tuple> buffer;

    public BufferingTupleListener(List<Tuple> buffer) {
        this.buffer = buffer;
    }

    @Override
    public void receiveTuples(List<Tuple> tuples) {
        synchronized (buffer) {
            buffer.addAll(tuples);
        }
    }

    @Override
    public void finished() {
        // Don't need to do anything ...
    }
}
